<?php
class CheckService {
    /**
     * @var CheckService
     */
    private static $instance;

    /**
     * Get the instance of CheckService.
     *
     * @return CheckService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new CheckService();
        }
        return self::$instance;
    }

    public function __construct() {

    }

    //校验充值时间是否有效
    public function checkRechargeTime($userId, $platform, $type, $indexId, $reqTime){
        $res = array(
            'code' => 0,
            'startTime' => 0,
            'endTime' => 0
        );

        //验证玩家平台是否有效
        $storePlatform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        if ($storePlatform != $platform){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PLATFORM_ERR'];//平台错误;
            return $res;
        }

        //验证类型所属平台是否有效????
        if ($type == 0){//微信
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PLATFORM_ERR'];//平台错误;
            return $res;
        }
        if ($type == 1 || $type == 2 || $type == 3){//非微信
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PLATFORM_ERR'];//平台错误;
            return $res;
        }

        $results = $this->getBuyTime($userId, $type, $indexId, $reqTime);
        if ($results['startTime'] == '' || $results['startTime'] == 0 || $results['startTime'] == null ||
        $results['endTime'] == '' || $results['endTime'] == 0 || $results['endTime'] == null){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GET_TIME_ERR'];//获取时间错误;
            return $res;
        }

        //礼包未开始售卖
        if ($reqTime <= $results['startTime']){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PLATFORM_ERR'];//未开始售卖
            return $res;
        }
        //礼包售卖结束
        if ($reqTime >= $results['endTime']){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['END_OF_SALE'];//售卖结束
            return $res;
        }

        $res['startTime'] = $results['startTime'];
        $res['endTime'] = $results['endTime'];
        return $res;
    }

    //获取礼包真实时间
    public function getBuyTime($userId, $type, $indexId, $reqTime){
        $temp = array();
         if ($type == 1){
             $temp = $this->rewardBranch($userId,$indexId,$reqTime);
         }
        if ($type == 2){
            $temp = $this->rechargeGiftPackBranch($userId,$indexId,$reqTime);
        }
        if ($type == 3){
            $temp = $this->rechargeBuyGIftPackBranch($userId,$indexId,$reqTime);
        }

        return array(
            'startTime' => $temp['startTime'] > 0 ? $temp['startTime'] : 0,
            'endTime' => $temp['endTime'] > 0 ? $temp['endTime'] : 0,
        );
    }

    //reward分支
    public function rewardBranch($userId,$indexId,$reqTime){
        $res = array(
            'startTime' => 0,
            'endTime' => 0
        );
        $diamondSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_DIAMOND_VALUE, $indexId, true);
        if (!$diamondSetting) {
            return $res;
        }

        // 每日礼包 && 每周礼包
        if ($diamondSetting['type'] == 6 || $diamondSetting['type'] == 9) {
            /**
             * @var  $dailyGiftPackList DailyGiftPackVoList
             * @var $dailyGiftPack DailyGiftPackVo
             */
            $dailyGiftPackList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_DAILY_GIFT_PACK);
            $dailyGiftPack = $dailyGiftPackList->getElement($indexId,false);
            if (!$dailyGiftPack) {
                return $res;
            }

            $updateTime = $dailyGiftPack->getUpdateTime();
            if($diamondSetting['type'] == 6) {
                if (date('Ymd',$updateTime) == date('Ymd',$reqTime)) {
                    return $res;
                }
                $today = strtotime(date('Y-m-d',$reqTime));
                $res['startTime'] = $today;
                $res['endTime'] = $today + 86400;
            }

            if($diamondSetting['type'] == 9) {
                $weekday = date('w');
                $weekday = ($weekday + 6) % 7;
                $date = date('Y-m-d',strtotime("-{$weekday} day"));
                $startTime = strtotime($date);
                $endTime = $startTime + 86400 * 7;
                if ($startTime <= $updateTime && $updateTime < $endTime) {
                    return $res;
                }
                $res['startTime'] = $startTime;
                $res['endTime'] = $endTime;
            }
        }

        // 周年庆
        if ($diamondSetting['type'] == 13) {
            //验证活动是否开启
            $anniversaryAid = ActivityAnniversaryService::get()->getAnniversaryIndexId($userId);
            if ($anniversaryAid) {
                /**
                 * @var AnniversaryStepVoList $anniversaryStepList
                 * @var AnniversaryStepVo $anniversaryStep
                 */
                //获取玩家当前购买阶梯礼包的数据
                $anniversaryStepList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_ANNIVERSARY_STEP);
                $anniversaryStep = $anniversaryStepList->getElement($anniversaryAid,false);
                if ($anniversaryStep){
                    $res['startTime'] = $anniversaryStep->getCreateTime();
                    $res['endTime'] = $anniversaryStep->getEndTime();
                }
                else{
                    return $res;
                }
            }
        }

        //合约系统
        if ($diamondSetting['type'] == 15 || $diamondSetting['type'] == 16) {
            //平台活动时间区分
            $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
            $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
            //获取合约系统的相关配置
            $contractConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, null, true);
            foreach ($contractConf as $aid=>$conf){
                if ($conf['functionId'] == GameConst::FUNC_CONTRACT_FUNC){
                    //验证合约期数是否正确
                    if ($platform != 'dev'){
                        $contractStart = strtotime($conf[$platformPre.'TriggerTime']);
                        $contractEnd = strtotime($conf[$platformPre.'EndTime']);
                    }
                    else{
                        $contractStart = strtotime($conf['weixinTriggerTime']);
                        $contractEnd = strtotime($conf['weixinEndTime']);
                    }

                    if ($reqTime >= $contractStart && $reqTime <= $contractEnd){
                        $res['startTime'] = $contractStart;
                        $res['endTime'] = $contractEnd;
                        break;
                    }
                }
            }
        }

        return $res;
    }

    //rechargeBuyGIftPack分支
    public function rechargeBuyGIftPackBranch($userId,$indexId,$reqTime){
        $res = array(
            'startTime' => 0,
            'endTime' => 0
        );
        $diamondSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_DIAMOND_VALUE, $indexId, true);
        if (!$diamondSetting) {
            return $res;
        }

        // 每日礼包 && 每周礼包
        if ($diamondSetting['type'] == 6 || $diamondSetting['type'] == 9) {
            /**
             * @var  $dailyGiftPackList DailyGiftPackVoList
             * @var $dailyGiftPack DailyGiftPackVo
             */
            $dailyGiftPackList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_DAILY_GIFT_PACK);
            $dailyGiftPack = $dailyGiftPackList->getElement($indexId,false);
            if (!$dailyGiftPack) {
                return $res;
            }

            $updateTime = $dailyGiftPack->getUpdateTime();
            if($diamondSetting['type'] == 6) {
                if (date('Ymd',$updateTime) == date('Ymd',$reqTime)) {
                    return $res;
                }
                $today = strtotime(date('Y-m-d',$reqTime));
                $res['startTime'] = $today;
                $res['endTime'] = $today + 86400;
            }

            if($diamondSetting['type'] == 9) {
                $weekday = date('w');
                $weekday = ($weekday + 6) % 7;
                $date = date('Y-m-d',strtotime("-{$weekday} day"));
                $startTime = strtotime($date);
                $endTime = $startTime + 86400 * 7;
                if ($startTime <= $updateTime && $updateTime < $endTime) {
                    return $res;
                }
                $res['startTime'] = $startTime;
                $res['endTime'] = $endTime;
            }
        }

        // 周年庆
        if ($diamondSetting['type'] == 13) {
            //验证活动是否开启
            $anniversaryAid = ActivityAnniversaryService::get()->getAnniversaryIndexId($userId);
            if ($anniversaryAid) {
                /**
                 * @var AnniversaryStepVoList $anniversaryStepList
                 * @var AnniversaryStepVo $anniversaryStep
                 */
                //获取玩家当前购买阶梯礼包的数据
                $anniversaryStepList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_ACTIVITY_ANNIVERSARY_STEP);
                $anniversaryStep = $anniversaryStepList->getElement($anniversaryAid,false);
                if ($anniversaryStep){
                    $res['startTime'] = $anniversaryStep->getCreateTime();
                    $res['endTime'] = $anniversaryStep->getEndTime();
                }
                else{
                    return $res;
                }
            }
        }

        //合约系统
        if ($diamondSetting['type'] == 15 || $diamondSetting['type'] == 16) {
            //平台活动时间区分
            $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
            $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
            //获取合约系统的相关配置
            $contractConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, null, true);
            foreach ($contractConf as $aid=>$conf){
                if ($conf['functionId'] == GameConst::FUNC_CONTRACT_FUNC){
                    //验证合约期数是否正确
                    if ($platform != 'dev'){
                        $contractStart = strtotime($conf[$platformPre.'TriggerTime']);
                        $contractEnd = strtotime($conf[$platformPre.'EndTime']);
                    }
                    else{
                        $contractStart = strtotime($conf['weixinTriggerTime']);
                        $contractEnd = strtotime($conf['weixinEndTime']);
                    }

                    if ($reqTime >= $contractStart && $reqTime <= $contractEnd){
                        $res['startTime'] = $contractStart;
                        $res['endTime'] = $contractEnd;
                        break;
                    }
                }
            }
        }

        return $res;
    }







}